/**
 * DateUtils.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.demo.handphone.handphone.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 日期格式转换，处理工具
 */
public class DateUtils {
	
    public static final String MEITUAN_AUTH_METHOD = "MWS";
    public static final String HTTP_HEADER_TIME_ZONE = "GMT";
    public static final String HTTP_HEADER_DATE_FORMAT = "EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'z";

	//返回"2019-05-27 10:40:40"格式的时间
	public static String transferLongToTime(Long millSec) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date = new Date(millSec);
		return sdf.format(date);
	}

	/**
	 * 按照指定的格式，将日期类型对象转换成字符串，例如：yyyy-MM-dd,yyyy/MM/dd,yyyy/MM/dd hh:mm:ss
	 * 
	 * @param date
	 * @param pattern
	 *            格式
	 * @return
	 */
	public static String formatDate(Date date, String pattern) {
		SimpleDateFormat formater = new SimpleDateFormat(pattern, Locale.CHINA);
		return formater.format(date);
	}

	/**
	 * 按照指定的格式，将字符串转换成日期类型对象，例如：yyyy-MM-dd,yyyy/MM/dd,yyyy/MM/dd hh:mm:ss
	 * 
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static Date parseDate(String dateStr, String pattern) {
		SimpleDateFormat formater = new SimpleDateFormat(pattern);
		try {
			return formater.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将字符串（yyyy-MM-dd）解析成日期
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate(String dateStr) {
		return parseDate(dateStr, "yyyy-MM-dd");
	}

	/**
	 * 将字符串（yyyy-MM-dd hh:mm:ss）解析成日期
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate2(String dateStr) {
		return parseDate(dateStr, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 将字符串（yyyy-MM-dd hh:mm）解析成日期
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate3(String dateStr) {
		return parseDate(dateStr, "yyyy-MM-dd HH:mm");
	}

	/**
	 * 将字符串（yyyy-MM-dd hh）解析成日期
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate4(String dateStr) {
		return parseDate(dateStr, "yyyy-MM-dd HH");
	}

	/**
	 * 将字符串转换成日期
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseCommonDate(String dateStr) {
		Date date = parseDate2(dateStr);
		if (date != null) {
			return date;
		}
		date = parseDate3(dateStr);
		if (date != null) {
			return date;
		}
		date = parseDate4(dateStr);
		if (date != null) {
			return date;
		}
		date = parseDate(dateStr);
		if (date != null) {
			return date;
		}
		return date;
	}

	/**
	 * 获取当前年份
	 * 
	 * @return
	 */
	public static int getCurrentYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	/**
	 * 获取当前月份
	 * 
	 * @return
	 */
	public static int getCurrentMonth() {
		return Calendar.getInstance().get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取当前是本年的第几天
	 * 
	 * @return
	 */
	public static int getCurrentDayOfYear() {
		return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 比较两个日期相差的天数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long getDayDiffer(Date d1, Date d2) {
		long rel = d2.getTime() - d1.getTime();
		return Math.abs(rel / (1000 * 60 * 60 * 24));
	}

	/**
	 * 比较两个日期相差的小时
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long getHourDiffer(Date d1, Date d2) {
		long rel = d2.getTime() - d1.getTime();
		return Math.abs(rel / (1000 * 60 * 60));
	}

	/**
	 * 比较两个日期相差的分钟数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long getMinuteDiffer(Date d1, Date d2) {
		long rel = d2.getTime() - d1.getTime();
		return Math.abs(rel / (1000 * 60));
	}

	/**
	 * 比较两个日期相差的秒数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long getSecondDiffer(Date d1, Date d2) {
		long rel = d2.getTime() - d1.getTime();
		return Math.abs(rel / (1000));
	}

	/**
	 * 比较两个日期相差的毫秒
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long getDiffer(Date d1, Date d2) {
		long rel = d2.getTime() - d1.getTime();
		return Math.abs(rel);
	}

	/**
	 * 取得所给的日期的日开始时间(00:00:00,000)
	 * 
	 * @param day
	 *            要转换的日期
	 * @return 日开始时间
	 */
	public static Date getStartDateOfDay(Date day) {
		if (day == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(day);
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		return cal.getTime();
	}

	/**
	 * 取得所给的日期的日结束时间(23:59:59,000)
	 * 
	 * @param day
	 *            要转换的日期
	 * @return 日结束时间
	 */
	public static Date getEndDateOfDay(Date day) {
		if (day == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(day);
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		return cal.getTime();
	}

	/**
	 * 今天之后的num天的日期
	 * 
	 * @param num
	 * @return
	 */
	public static Date getDayAfterDay(int num) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, num);
		return cal.getTime();
	}

	public static boolean isWeekend(Date date) {
		return (date.getDay() == 5 || date.getDay() == 6);
	}

	public static boolean isWeekend(String dateStr) {
		Date date = parseDate(dateStr);
		return (date.getDay() == 5 || date.getDay() == 6);
	}

	/**
	 * 当前时间以指定格式传换成文字.
	 * 
	 * @param pattern
	 *            格式
	 * @return 文字
	 */
	public static String nowInFormat(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(Calendar.getInstance().getTime());
	}

	/**
	 * 时间比较, 比较到日
	 * 
	 * @param date1
	 *            时间1
	 * @param date2
	 *            时间2
	 * @return 0表示相同, 正数是date1 > date2, 负数是date1 < date2
	 */
	public static int compareToDay(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return 0;
		}
		if (date1 == null) {
			return -1;
		}
		if (date2 == null) {
			return 1;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date1).compareTo(sdf.format(date2));
	}

	/**
	 * 时间比较, 比较到时
	 * 
	 * @param date1
	 *            时间1
	 * @param date2
	 *            时间2
	 * @return 0表示相同, 正数是date1 > date2, 负数是date1 < date2
	 */
	public static int compareToHour(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return 0;
		}
		if (date1 == null) {
			return -1;
		}
		if (date2 == null) {
			return 1;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
		return sdf.format(date1).compareTo(sdf.format(date2));
	}

	/**
	 * 时间比较, 比较到秒
	 * 
	 * @param date1
	 *            时间1
	 * @param date2
	 *            时间2
	 * @return 0表示相同, 正数是date1 > date2, 负数是date1 < date2
	 */
	public static int compareToSecond(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return 0;
		}
		if (date1 == null) {
			return -1;
		}
		if (date2 == null) {
			return 1;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date1).compareTo(sdf.format(date2));
	}

	/**
	 * 时间比较, 比较时分秒
	 * 
	 * @param date1
	 *            时间1
	 * @param date2
	 *            时间2
	 * @return 0表示相同, 正数是date1 > date2, 负数是date1 < date2
	 */
	public static int compareHour(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return 0;
		}
		if (date1 == null) {
			return -1;
		}
		if (date2 == null) {
			return 1;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		return sdf.format(date1).compareTo(sdf.format(date2));
	}

	/**
	 * 得到本周的第一天
	 * 
	 * @return
	 */
	public static String getNowWeekFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar = getADayOfWeek(calendar, Calendar.MONDAY);

		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	private static Calendar getADayOfWeek(Calendar day, int dayOfWeek) {
		int week = day.get(Calendar.DAY_OF_WEEK);
		if (week == dayOfWeek)
			return day;
		int diffDay = dayOfWeek - week;
		if (week == Calendar.SUNDAY) {
			diffDay -= 7;
		} else if (dayOfWeek == Calendar.SUNDAY) {
			diffDay += 7;
		}
		day.add(Calendar.DATE, diffDay);
		return day;
	}

	/**
	 * 得到本周的最后一天
	 * 
	 * @return
	 */
	public static String getNowWeekLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar = getADayOfWeek(calendar, Calendar.SUNDAY);
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到当月第N周的第一天
	 * 
	 * @param monthDay
	 * @param weekIndex
	 * @return
	 */
	public static String getMonthWeekFirstDay(Date monthDay, int weekIndex) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(monthDay);
		calendar.set(Calendar.WEEK_OF_MONTH, weekIndex);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMinimum(Calendar.DAY_OF_WEEK) + 1);
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到当月第N周最后一天
	 * 
	 * @param monthDay
	 * @param weekIndex
	 * @return
	 */
	public static String getMonthWeekLastDay(Date monthDay, int weekIndex) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(monthDay);
		calendar.set(Calendar.WEEK_OF_MONTH, weekIndex);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
		calendar.add(Calendar.DATE, 1);
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}
	
	/**
	 * 得到上周
	 * 
	 * @param monthDay
	 * @param weekIndex
	 * @return
	 */
	public static String getMonthWeekFirstDayW(Date monthDay) {
		Calendar calendar = Calendar.getInstance();
		int week = calendar.get(Calendar.WEEK_OF_MONTH);//获取是本月的第几周
		if (Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
			week = week - 2;
		} else {
			week = week - 1;
		}
		calendar.setTime(monthDay);
		calendar.set(Calendar.WEEK_OF_MONTH, week);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMinimum(Calendar.DAY_OF_WEEK) + 1);
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到上周
	 * 
	 * @param monthDay
	 * @param weekIndex
	 * @return
	 */
	public static String getMonthWeekLastDayW(Date monthDay) {
		Calendar calendar = Calendar.getInstance();
		int week = calendar.get(Calendar.WEEK_OF_MONTH);//获取是本月的第几周
		if (Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
			week = week - 2;
		} else {
			week = week - 1;
		}
		calendar.setTime(monthDay);
		calendar.set(Calendar.WEEK_OF_MONTH, week);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
		calendar.add(Calendar.DATE, 1);
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到本月的第一天
	 * 
	 * @return
	 */
	public static String getNowMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到本月的最后一天
	 * 
	 * @return
	 */
	public static String getNowMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到当年第N月的第一天
	 * 
	 * @return
	 */
	public static String getYearMonthFirstDay(Date yearDay, int monthIndex) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(yearDay);
		calendar.set(Calendar.MONTH, monthIndex);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到当年第N月的最后一天
	 * 
	 * @return
	 */
	public static String getYearMonthLastDay(Date yearDay, int monthIndex) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(yearDay);
		calendar.set(Calendar.MONTH, monthIndex);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到一个月有几周
	 * 
	 * @return
	 */
	public static Integer getMonthWeekCount(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
	}

	/**
	 * 得到今年的第一天
	 * 
	 * @return
	 */
	public static String getNowYearFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMinimum(Calendar.DAY_OF_YEAR));

		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到今年的最后一天
	 * 
	 * @return
	 */
	public static String getNowYearLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到上个月的最后一天
	 * 
	 * @return
	 */
	public static String getUpMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1); // 月份减1
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 得到现在的时间
	 * 
	 * @return Date
	 */
	public static Date now() {
		return Calendar.getInstance().getTime();
	}

	/**
	 * 得到现在的时间"yyyy-MM-dd"
	 * 
	 * @return
	 */
	public static String nowDate() {
		Date now = new Date();
		return formatDate(now, "yyyy-MM-dd");
	}

	/**
	 * 得到现在的时间"yyyy-MM-dd 00:00:00"
	 * 
	 * @return
	 */
	public static String nowDateStart() {
		Date now = new Date();
		return formatDate(now, "yyyy-MM-dd 00:00:00");
	}

	/**
	 * 得到现在的时间"yyyy-MM-dd HH:mm:ss"
	 * 
	 * @return
	 */
	public static String nowTimeStart() {
		Date now = new Date();
		return formatDate(now, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 得到现在的时间"yyyy-MM-dd HH:mm:ss.SSS"
	 * 
	 * @return
	 */
	public static String nowTimeMS() {
		Date now = new Date();
		return formatDate(now, "yyyy-MM-dd HH:mm:ss.SSS");
	}
	
	/**
	 * 得到现在的时间"yyyy-MM-dd 23:59:59"
	 * 
	 * @return
	 */
	public static String nowDateEnd() {
		Date now = new Date();
		return formatDate(now, "yyyy-MM-dd 23:59:59");
	}

	/**
	 * 当前日期（date）的num天的日期
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getDayAfterDay(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, num);
		return cal.getTime();
	}

	/**
	 * 当前日期（date）的num天的日期
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @param num
	 * @return
	 */
	public static Date getDayAfterDay(String dateStr, int num) {
		Date date = parseDate(dateStr);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, num);
		return cal.getTime();
	}

	/**
	 * 获取当前日期是星期几
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static int dayForWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	}

	/**
	 * 获取星期几
	 * 
	 * @param date
	 * @return
	 */
	public static String getWeekDayName(Date date) {
		String strWeek = "";
		int day = DateUtils.dayForWeek(date);
		switch (day) {
		case 1:
			strWeek += "星期一";
			break;
		case 2:
			strWeek += "星期二";
			break;
		case 3:
			strWeek += "星期三";
			break;
		case 4:
			strWeek += "星期四";
			break;
		case 5:
			strWeek += "星期五";
			break;
		case 6:
			strWeek += "星期六";
			break;
		case 7:
			strWeek += "星期日";
			break;
		}
		return strWeek;
	}

	/**
	 * 当前日期（date）的num小时后的日期
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getDayAfterHour(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// cal.add(Calendar.DATE,num);
		cal.add(Calendar.HOUR, num);
		return cal.getTime();
	}

	/**
	 * 当前日期（date）的num分钟后的日期
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getDayAfterMinute(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// cal.add(Calendar.DATE,num);
		cal.add(Calendar.MINUTE, num);
		return cal.getTime();
	}
	
	/**
	 * 当前日期（date）的num秒后的日期
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getDayAfterSecond(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, num);
		return cal.getTime();
	}

	/**
	 * 当前日期（date）的num天后的日期
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getDayAfterDate(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, num);
		return cal.getTime();
	}

	/**
	 * 毫秒转时间字符串
	 * 
	 * @param millis
	 * @param pattern
	 * @return
	 */
	public static String toDateString(Long millis, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millis);
		return DateUtils.formatDate(cal.getTime(), pattern);
	}

	/**
	 * 判断两组时间是否存在交集
	 * 
	 * @author Liay
	 * @date 2015-6-22 下午4:16:20
	 * @param startTime
	 * @param endTime
	 * @param oldStartTime
	 * @param oldEndTime
	 * @return
	 */
	public static boolean checkIntersection(Date startTime, Date endTime, Date oldStartTime, Date oldEndTime) {
		if ((compareToSecond(startTime, oldStartTime) >= 0 && compareToSecond(startTime, oldEndTime) <= 0) || (compareToSecond(endTime, oldStartTime) >= 0 && compareToSecond(endTime, oldEndTime) <= 0) || (compareToSecond(startTime, oldStartTime) >= 0 && compareToSecond(endTime, oldEndTime) <= 0) || (compareToSecond(startTime, oldStartTime) <= 0 && compareToSecond(endTime, oldEndTime) >= 0)) {
			return true;
		}
		return false;
	}

	/**
	 * 获取指定时间月的第一天
	 * 
	 * @author dqwu
	 * @date 2015-6-29 下午8:52:26
	 * @param dateTime
	 * @return
	 */
	public static String getMonthFirstDayByTime(Date dateTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTime);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 获取指定时间月的最后一天
	 * 
	 * @author dqwu
	 * @date 2015-6-29 下午8:52:26
	 * @param dateTime
	 * @return
	 */
	public static String getMonthLastDayByTime(Date dateTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTime);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 获取指定时间年的第一天
	 * 
	 * @author dqwu
	 * @date 2015-6-29 下午8:52:26
	 * @param dateTime
	 * @return
	 */
	public static String getYearFirstDayByTime(Date dateTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTime);
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMinimum(Calendar.DAY_OF_YEAR));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 获取指定时间年的最后一天
	 * 
	 * @author dqwu
	 * @date 2015-6-29 下午8:52:26
	 * @param dateTime
	 * @return
	 */
	public static String getYearLastDayByTime(Date dateTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTime);
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
		return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 获取两个时间相差的时间字符串，供调试时间差使用
	 * @author dqwu
	 * @date 2015-7-27 上午9:47:01
	 * @param startTimeMillis 开始毫秒数   在代码开始加上：long startTimeMillis = System.currentTimeMillis();
	 * @param endTimeMillis 结束毫秒数   在代码结尾加上：long endTimeMillis = System.currentTimeMillis();
	 * @return
	 */
	public static String getTimeDifferenceString(long startTimeMillis, long endTimeMillis) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(endTimeMillis - startTimeMillis);

		return "耗时: " + c.get(Calendar.MINUTE) + "分 " + c.get(Calendar.SECOND) + "秒 " + c.get(Calendar.MILLISECOND) + " 毫秒";
	}

	/**
	 * 将格式为 yyyyMMddHHmmss 装换为yyyy-MM-dd HH:mm:ss
	 * @author sw
	 * @date 2016-3-28 下午2:41:28
	 * @param dateStr 格式为 yyyyMMddHHmmss 时间字符串
	 * @return
	 */
	public static Date getDate(String dateStr) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
	    Date date = null;
	    try {
	        date = df.parse(dateStr); 
	        return date;
	    } catch (Exception ex) {
	    	System.out.println(ex.getMessage());
	    }
		return date; 
	}
	
	/**
	 * 验证时间是否完全被包含
	 * true 完全包含，false 没完全被包含
	 * @author sw
	 * @date 2016-3-30 上午10:54:12
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param includedStartTime 被包含的开始时间 
	 * @param includedEndTime 被包含的结束时间 
	 * @return
	 */
	public static boolean checkContainDate(Date startTime, Date endTime, Date includedStartTime, Date includedEndTime) {
		if (DateUtils.compareToDay(startTime, includedStartTime) <= 0 && DateUtils.compareToDay(endTime, includedEndTime) >= 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 获取上个月的第一天
	  * @author wxg
	  * @date 2016-5-7 下午7:43:07 
	  * @return
	  * @throws Exception
	 */
	public static String getBeforeFirstMonthdate(){
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return format.format(calendar.getTime());
	}
	/**
	 * 获取上个月的最后一天
	  * @author wxg
	  * @date 2016-5-7 下午7:43:16 
	  * @throws Exception
	 */
	public static String getBeforeLastMonthdate(){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		int month=calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return sf.format(calendar.getTime());
	}
	
	/**
	 * 获取某个月共多少天
	  * @author wxg
	  * @date 2016-6-14 下午2:28:39 
	  * @param dateStr 2016-06
	  * @return 0 为不成功
	 */
	public static int getDayForMonth(String dateStr) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Calendar calendar = new GregorianCalendar();
			Date date1 = sdf.parse(dateStr);
			calendar.setTime(date1); //放入你的日期 
			return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println("获取失败！");
		} 
		return 0;
	}

	/**
	 * @author wxg
	 * @date 2016-11-28 下午7:43:58
	 * @param date
	 * @return
	 */
	public static Date getDateGMT(String date) {
		SimpleDateFormat df = new SimpleDateFormat(HTTP_HEADER_DATE_FORMAT, Locale.US);
        df.setTimeZone(TimeZone.getTimeZone(HTTP_HEADER_TIME_ZONE));
        Date executeTime = null;
        try {
			executeTime = df.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return executeTime;
	}
	
}
